package com.spring.pick2pick.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.ibatis.SqlMapClientTemplate;
import org.springframework.stereotype.Repository;

import com.spring.pick2pick.common.PageHandler;
import com.spring.pick2pick.model.LeagueItem;

/** 리그의 기능을 처리하는 Dao */
@Repository
public class LeagueDao {
	
	@Autowired
	private SqlMapClientTemplate sqlMapClientTemplate;
	
	/** 리그 종목 */
	public String getSportName(int idSport) {
		return (String) sqlMapClientTemplate.queryForObject("league.getSportName", idSport);
	}
	
	/** 리그 추가 기능 */
	public int addLeague(LeagueItem item) {
		return (Integer) sqlMapClientTemplate.insert("league.addLeague", item);
	}
	
	/** 리그 수정 기능 */
	public int editLeague(LeagueItem item) {
		return (Integer) sqlMapClientTemplate.update("league.editLeague", item);
	}
	
	/** 활성 리그 카운트 */
	public int getDisplayCount(int idSport) {
		return (Integer) sqlMapClientTemplate.queryForObject("league.getDisplayCount", idSport);
	}
	
	/** 비활성 리그 카운트 */
	public int getUnDisplayCount(int idSport) {
		return (Integer) sqlMapClientTemplate.queryForObject("league.getUnDisplayCount", idSport);
	}
	
	/** 리그 리스트 기능 */
	@SuppressWarnings("unchecked")
	public List<LeagueItem> getLeagueList(int page, int size, int idSport, String order) {
		
		int total = (Integer) sqlMapClientTemplate.queryForObject("league.getLeagueListCount", idSport);
		
		if (size == -1) {
			size = total;
		}
		
		if (total == 0) {
			PageHandler.freeInstance();
		} else {
			PageHandler.getInstance(page, total, size, 10);
		}
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("start", PageHandler.getInstance().getStartRec());
		map.put("end", PageHandler.getInstance().getPageArticleCount());
		map.put("idSport", idSport);
		map.put("order", order);
		
		List<LeagueItem> list = sqlMapClientTemplate.queryForList("league.getLeagueList", map);
		
		return list;
	}
	
}
